package com.adp.interceptor.roworder;

import com.adf.config.Config;
import com.adf.config.component.application.report.ReportBean;
import com.adf.system.ReportRequest;
import com.adf.system.commoninterface.IListReportRoworderPersistence;
import com.adf.util.Consts;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;


public class objStructStoreRowOrderByDrag implements IListReportRoworderPersistence {

	public String loadRoworder(Map<String, String> mColValuesInRow) {
		 return mColValuesInRow.get("orderline");
	}

	public void storeRoworderByArrow(ReportRequest arg0, ReportBean arg1,
                                     Map<String, String> arg2, Map<String, String> arg3, boolean arg4) {
		// TODO Auto-generated method stub
		
	}

	public void storeRoworderByDrag(ReportRequest rrequest, ReportBean rbean,
                                    Map<String, String> mColValuesInRow, Map<String, String> mColValuesInDestRow, boolean direct) {
		/**
         * 因为no、orderline两个<col/>都配置了rowordervalue为true，所以可以从中取到它们的值
         */
		rrequest.getWResponse().addOnloadMethod("orderby", "", false);
        String ID=mColValuesInRow.get("ID");//取到被排序记录行的no列的值（因为column为no的<col/>的rowordervalue属性值为true）
        String myOrderline=mColValuesInRow.get("fieldorder");//取到被排序记录行的no列的值（因为column为no的<col/>的rowordervalue属性值为true）
        String destOrderline=mColValuesInDestRow.get("fieldorder");//取到目标位置排序列的值
        int idestOrderline;
        try
        {
            idestOrderline=Integer.parseInt(destOrderline);
        }catch(NumberFormatException e1)
        {
            idestOrderline=0;
        }
        Connection conn=null;
        Statement stmt=null;
        try
        {
            conn= Config.getInstance().getDataSource(rbean.getSbean().getDatasource()).getConnection();
            stmt=conn.createStatement();
            ResultSet rs=null;
            int iorderline=0;
            String sql="";
            if(direct)
            {//如果是移到目标记录行的上方
               // String sql="select fieldorder from tbl_s_table_struct where fieldorder<"+destOrderline+" order by fieldorder desc";//取到目标记录行上面一行的排序值，稍后要移到它们中间
            	sql="update tbl_s_table_struct set  fieldorder=fieldorder+1 where tableid=(select top 1 tableid from tbl_s_table_struct where ID='"+ID+"') and fieldorder>="+destOrderline+" and fieldorder<"+myOrderline+"";
           
            }else
            {//移到被排序记录行的下方
            	sql="update tbl_s_table_struct set  fieldorder=fieldorder-1 where tableid=(select top 1 tableid from tbl_s_table_struct where ID='"+ID+"') and fieldorder<="+destOrderline+" and fieldorder>"+myOrderline+"";
            	
            }
            stmt.executeUpdate(sql);
            stmt.executeUpdate("update tbl_s_table_struct set fieldorder="+destOrderline+" where ID='"+ID+"'");
            rrequest.getWResponse().getMessageCollector().success("排序记录行数据成功");
        }catch(SQLException e)
        {
            e.printStackTrace();
            rrequest.getWResponse().setStatecode(Consts.STATECODE_FAILED);
            rrequest.getWResponse().getMessageCollector().warn("排序报表记录行失败",null,true);
        }finally
        {
            //WabacusAssistant.getInstance().release(conn,stmt);
        }
		
	}

	public void storeRoworderByInputbox(ReportRequest arg0, ReportBean arg1,
                                        Map<String, String> arg2, String arg3) {
		// TODO Auto-generated method stub
		
	}

	public void storeRoworderByTop(ReportRequest arg0, ReportBean arg1,
                                   Map<String, String> arg2) {
		// TODO Auto-generated method stub
		
	}
	
	
}
